var Edit = {
		data: {},
		modelPath: "/templateHtml",
		layerIndex: -1
}

Edit.save = function(){
	Edit.collectData();
	var ajax = new $ax(Base.ctxPath + Edit.modelPath+"/save", function(data){
		Tip.success("操作成功!");
		window.parent.Index.table.refresh();
		Edit.close();
	},function(data){
		Tip.error("操作失败!" + data.responseJSON.message + "!");
		setTimeout(function(){
			$("#submitBtn").removeAttr("disabled");
		},200); 
	});
	ajax.setContentType('application/json;charset=utf-8');
	ajax.setData(JSON.stringify(Edit.data));
	ajax.start();
}
Edit.submit = function(){
	Tip.confirm("确认提交？", function(){
		Edit.collectData();
		var ajax = new $ax(Base.ctxPath + Edit.modelPath+"/submit", function(data){
			Tip.success("操作成功!");
			window.parent.Index.table.refresh();
			Edit.close();
		},function(data){
			Tip.error("操作失败!" + data.responseJSON.message + "!");
			setTimeout(function(){
				$("#submitBtn").removeAttr("disabled");
			},200); 
		});
		ajax.setContentType('application/json;charset=utf-8');
		ajax.setData(JSON.stringify(Edit.data));
		ajax.start();
	});
}

Edit.close = function () {
	window.parent.layer.close(window.parent.Index.layerIndex);
};

Edit.collectData = function () {
	$(".form-control").each(function(i,v){ 
		Edit.data[v.id] = v.value;
	})
	var text = editor.getValue();
	Edit.data['htmlText'] = text;
	var paramIds = $('.noselect2').selectpicker('val');
	if(paramIds){
		var paramIdstr = '';
		for(var i=0;i<paramIds.length;i++){
			paramIdstr += paramIds[i]+',';
		}
		Edit.data['paramIdstr'] = paramIdstr;
	}
	var signList = $("#signTables").bootstrapTable('getData')
	if(signList&&signList.length>0){
//		var arrs = [];
//		for(var i=0;i<signList.length;i++){
//			arrs.push(signList[i]);
//		}
		Edit.data['signList'] = signList;
	}
//	alert(JSON.stringify($("#signTables").bootstrapTable('getData')));
};

Edit.clearData = function() {
	$(".form-control").each(function(i,v){ 
		$("#"+v.id).val("");
	})
}

Edit.genModelHtml = function(gm) {
	if(gm){
		if(gm=='HTML'){
			$("#htmltr").attr("style","display:display;")
			$("#filetr").attr("style","display:none;")
		}else if(gm=='DOC'||gm=='PDF'){
			$("#htmltr").attr("style","display:none;")
			$("#filetr").attr("style","display:display;")
		}
	}
}

$(function(){
	//表单校验
	$("#submitForm").validate({
		rules: {
		}, 
		messages: {
		},
		submitHandler:function(form){
			Edit.submit();
		}
	});

	$("#saveBtn").on("click", function(){
		Edit.save();
	});
	$("#tryHtmlBtn").on("click", function(){
		Edit.tryHtml();
	});

	$("#genModel").on("change", function(){
		Edit.genModelHtml(this.value);
	});

	Edit.genModelHtml($("#genModel").val());

	$("#signGet").click(function() {
		alert(JSON.stringify($("#signTables").bootstrapTable('getData')));
	});

	$("#signAdds").click(function() {
		var index = layer.open({
			type: 2,
			title: '新建/编辑签名',
			maxmin: false,
			move: false,
			area: ['55%', '45%'],
			content: Base.ctxPath + '/signature/signEdit'
		});
		this.layerIndex = index;
	});
});
